﻿using MiniFox.Data.NativeClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace MiniFox.Data.MySql
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class MySqlDataQuery<TEntity> : NativeDataQuery<TEntity> where TEntity : class
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="database"></param>
        public MySqlDataQuery(Database database) : base(database)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mainSql"></param>
        /// <param name="sortFields"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        protected override string CreateCommandTextForPagedQuery(string mainSql, IDictionary<string, bool> sortFields, int pageIndex, int pageSize)
        {
            string sortStatement = DatabaseUtil.ResolveSortExpression(sortFields);
            string sql = $"{mainSql} {sortStatement} limit {(pageIndex - 1) * pageSize}, {pageSize}";
            return sql;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mainSql"></param>
        /// <param name="sortFields"></param>
        /// <param name="maxCount"></param>
        /// <returns></returns>
        protected override string CreateCommandTextForLimitedQuery(string mainSql, IDictionary<string, bool> sortFields, int maxCount)
        {
            string sortStatement = DatabaseUtil.ResolveSortExpression(sortFields);
            string sql = $"{mainSql} {sortStatement} limit 0, {maxCount}";
            return sql;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="queryable"></param>
        /// <param name="expression"></param>
        /// <param name="sortFields"></param>
        /// <param name="maxRowCount"></param>
        /// <returns></returns>
        public override ResultSet<TEntity> Execute(IQueryable<TEntity> queryable, Expression<Func<TEntity, bool>> expression, IDictionary<string, bool> sortFields, int? maxRowCount)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="queryable"></param>
        /// <param name="expression"></param>
        /// <param name="sortFields"></param>
        /// <returns></returns>
        public override ResultSet<TEntity> Execute(IQueryable<TEntity> queryable, Expression<Func<TEntity, bool>> expression, IDictionary<string, bool> sortFields)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="queryable"></param>
        /// <param name="expression"></param>
        /// <param name="sortFields"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public override PagedResultSet<TEntity> Execute(IQueryable<TEntity> queryable, Expression<Func<TEntity, bool>> expression, IDictionary<string, bool> sortFields, int pageIndex, int pageSize)
        {
            throw new NotImplementedException();
        }
    }
}
